
//
//  ST_RefuseLoveViewController.swift
//  btvideo
//
//  Created by JarvanZhang on 2020/2/5.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyJSON
import HandyJSON
import MBProgressHUD
import MJRefresh


class ST_RefuseLoveViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        automaticallyAdjustsScrollViewInsets = false
        
        title_nav_btn.setTitleForAllStates("拒绝的喜欢")
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(own_navigation_bar.snp.bottom)
        }
        
        view.addSubview(emptyView)
        emptyView.snp.makeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.height.equalTo(emptyView.figured_height)
            make.centerY.equalToSuperview().offset( iPhoneXSeries ? -70 : -80)
        }
        
        tableView.backgroundColor = Color.white
        
        self.request_data()
        
    }
        
    func request_data(){
        
        currentPage += 1
        
        JVrequest(PH.refundLikeListApi,parameters:["page":currentPage,"size":15], success: { (json) in
            
                let array = json["data"].arrayValue
                
                print("拒绝的喜欢Array:",array)
            
                 if self.currentPage == 1{
                    self.data_array = array
                 }else{
                    self.data_array += array
                }
            
            
            self.load_more_option_style(tableView: self.tableView, count: array.count, pageSize: 15) {
                
                self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in

                    self?.request_data()
                })
            }

            self.reload_with_empty_view()
            
            self.tableView.mj_footer?.endRefreshing()
            
        }) { (code, err) in
            self.currentPage -= 1
            
            self.tableView.mj_footer?.endRefreshing()
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
//    MARK:- Properties
    
    var currentPage = 0
    
    fileprivate lazy var tableView:UITableView = {
        
        let control = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        
        let bottomEdge = iPhoneXSeries ? BTSafeAreaBottom + 10 : 30
        
        control.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: bottomEdge, right: 0)
        
        control.delegate = self
        
        control.dataSource = self
        
//        control.rowHeight = UITableView.automaticDimension
        
//        control.estimatedRowHeight = 92
        
        control.register(cellWithClass: ST_RefuseLoveCell.self)
        
        control.separatorStyle = .none
        
        control.allowsMultipleSelectionDuringEditing = true
        
        control.backgroundColor = Color.white
        //        control.backgroundColor = Color.random
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        return control
    }()
    
    
    fileprivate lazy var emptyView:ST_BTEmptyView = {
           
       let control = ST_BTEmptyView.init(title: "这里可以重新喜欢你左滑的对象", imageName: ST_ImageUtils.imageName("喜欢_img_拒绝的喜欢(空)")!)
       control.isHidden = true
       return control
   }()
    
    fileprivate var data_array = [JSON]()
    
}


extension ST_RefuseLoveViewController:UITableViewDelegate,UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withClass: ST_RefuseLoveCell.self, for: indexPath)
        
        cell.bind_model(json: data_array[indexPath.row])

//        cell.acceipt_closure = {[weak self] m in
//            self?.re_love_handle(json: m)
//        }
        
        cell.removeClosure = {[weak self] info in
            
            self?.data_array.removeFirst { (itemJson) -> Bool in
                return itemJson["accid"].stringValue == info["accid"].stringValue
            }
            
            self?.reload_with_empty_view()
        }
        
        return cell
    }
    
    
    fileprivate func reload_with_empty_view(){
        
        
        self.tableView.reloadData()
        
        self.emptyView.isHidden = self.data_array.count > 0
        self.tableView.isHidden = self.data_array.count < 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = data_array[indexPath.item]["accid"].stringValue
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    
}


//MARK: - Event
extension ST_RefuseLoveViewController{
    
    fileprivate func re_love_handle(json:JSON){
        
        let info = ST_MatchUserInfo.init(accid: json["accid"].stringValue, avatar: json["avatar"].stringValue, nickname: json["nickname"].stringValue)
        
        JVrequest(PH.receiveAgainPersonApi,parameters: ["target_accid":info.accid] ,success: { (json) in
            
            print("喜欢某个用户:\(json.rawString() ?? "空数据")")
            
            if json["data"]["mutual_like"].boolValue{
                ST_BTBootController.shared.matchSuccess(info: info)
            }
            
            self.data_array.removeFirst { (itemJson) -> Bool in
                return itemJson["accid"].stringValue == info.accid
            }
            
            self.reload_with_empty_view()
            
        }) { (code, err) in
            
            print("当前 返回喜欢不喜欢 错误信息：",err)
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
}
